package com.bdmc.device.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.device.domain.bo.DevFacilityHistoryBo;
import com.bdmc.device.domain.vo.DevFacilityHistoryVo;
import com.bdmc.device.domain.DevFacilityHistory;
import com.bdmc.device.mapper.DevFacilityHistoryMapper;
import com.bdmc.device.service.IDevFacilityHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 满溢设备历史数据Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-22
 */
@Service
public class DevFacilityHistoryServiceImpl extends ServicePlusImpl<DevFacilityHistoryMapper, DevFacilityHistory, DevFacilityHistoryVo> implements IDevFacilityHistoryService {

    @Override
    public DevFacilityHistoryVo queryById(Integer historyId){
        return getVoById(historyId);
    }

    @Override
    public TableDataInfo<DevFacilityHistoryVo> queryPageList(DevFacilityHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<DevFacilityHistory> lqw = buildQueryWrapper(bo);
        Page<DevFacilityHistoryVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<DevFacilityHistoryVo> mySelectPage(DevFacilityHistoryBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<DevFacilityHistoryVo> queryList(DevFacilityHistoryBo bo) {
        LambdaQueryWrapper<DevFacilityHistory> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<DevFacilityHistory> buildQueryWrapper(DevFacilityHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<DevFacilityHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeviceId() != null, DevFacilityHistory::getDeviceId, bo.getDeviceId());
        lqw.eq(bo.getDataTime() != null, DevFacilityHistory::getDataTime, bo.getDataTime());
        lqw.eq(StringUtils.isNotBlank(bo.getRssi()), DevFacilityHistory::getRssi, bo.getRssi());
        lqw.eq(StringUtils.isNotBlank(bo.getSnr()), DevFacilityHistory::getSnr, bo.getSnr());
        lqw.eq(StringUtils.isNotBlank(bo.getVoltage()), DevFacilityHistory::getVoltage, bo.getVoltage());
        lqw.eq(bo.getDistance() != null, DevFacilityHistory::getDistance, bo.getDistance());
        lqw.eq(StringUtils.isNotBlank(bo.getRsrp()), DevFacilityHistory::getRsrp, bo.getRsrp());
        lqw.eq(bo.getTemperature() != null, DevFacilityHistory::getTemperature, bo.getTemperature());
        lqw.eq(bo.getSign() != null, DevFacilityHistory::getSign, bo.getSign());
        lqw.eq(bo.getOverflow() != null, DevFacilityHistory::getOverflow, bo.getOverflow());
        lqw.eq(bo.getFrameCount() != null, DevFacilityHistory::getFrameCount, bo.getFrameCount());
        lqw.eq(bo.getFire() != null, DevFacilityHistory::getFire, bo.getFire());
        lqw.eq(bo.getFall() != null, DevFacilityHistory::getFall, bo.getFall());
        lqw.eq(bo.getCell() != null, DevFacilityHistory::getCell, bo.getCell());
        lqw.eq(bo.getBatteryQuantityStatus() != null, DevFacilityHistory::getBatteryQuantityStatus, bo.getBatteryQuantityStatus());
        lqw.eq(bo.getAngle() != null, DevFacilityHistory::getAngle, bo.getAngle());
        return lqw;
    }

    @Override
    public Boolean insertByBo(DevFacilityHistoryBo bo) {
        DevFacilityHistory add = BeanUtil.toBean(bo, DevFacilityHistory.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setHistoryId(add.getHistoryId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(DevFacilityHistoryBo bo) {
        DevFacilityHistory update = BeanUtil.toBean(bo, DevFacilityHistory.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(DevFacilityHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
